package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TcClass;
import com.zhentao.service.TcClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/class")
public class ClassController {

    @Autowired
    TcClassService classService;

    /**
     * 获取全部班级
     */
    @GetMapping("list")
    public List<TcClass> list(){
        return classService.list();
    }

    /**
     * 根据ID获取班级详情
     */
    @GetMapping("get/{id}")
    public TcClass getById(@PathVariable Integer id){
        return classService.getById(id);
    }

    /**
     * 添加班级
     */
    @PostMapping("add")
    public Map<String, Object> add(@RequestBody TcClass tcClass){
        Map<String, Object> result = new HashMap<>();
        try {
            tcClass.setCreateTime(new Date());
            boolean success = classService.save(tcClass);
            result.put("success", success);
            result.put("message", success ? "添加成功" : "添加失败");
            if (success) {
                result.put("data", tcClass);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新班级
     */
    @PutMapping("update")
    public Map<String, Object> update(@RequestBody TcClass tcClass){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = classService.updateById(tcClass);
            result.put("success", success);
            result.put("message", success ? "更新成功" : "更新失败");
            if (success) {
                result.put("data", tcClass);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除班级
     */
    @DeleteMapping("delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = classService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据创建者ID获取班级
     */
    @GetMapping("myClasses/{creatorUserId}")
    public List<TcClass> getMyClasses(@PathVariable Integer creatorUserId){
        QueryWrapper<TcClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creator_user_id", creatorUserId);
        queryWrapper.orderByDesc("create_time");
        return classService.list(queryWrapper);
    }

    /**
     * 根据年级获取班级
     */
    @GetMapping("byGrade/{grade}")
    public List<TcClass> getByGrade(@PathVariable String grade){
        QueryWrapper<TcClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("grade", grade);
        queryWrapper.orderByDesc("create_time");
        return classService.list(queryWrapper);
    }

    /**
     * 根据科目获取班级
     */
    @GetMapping("bySubject/{subject}")
    public List<TcClass> getBySubject(@PathVariable String subject){
        QueryWrapper<TcClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject", subject);
        queryWrapper.orderByDesc("create_time");
        return classService.list(queryWrapper);
    }

    /**
     * 搜索班级
     */
    @GetMapping("search")
    public List<TcClass> searchClasses(@RequestParam String keyword){
        QueryWrapper<TcClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("class_name", keyword).or().like("grade", keyword).or().like("subject", keyword);
        queryWrapper.orderByDesc("create_time");
        return classService.list(queryWrapper);
    }

    /**
     * 获取班主任班级
     */
    @GetMapping("headteacherClasses")
    public List<TcClass> getHeadteacherClasses(){
        QueryWrapper<TcClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_headteacher", 1);
        queryWrapper.orderByDesc("create_time");
        return classService.list(queryWrapper);
    }

    /**
     * 根据班级邀请码获取班级
     */
    @GetMapping("byCode/{classCode}")
    public TcClass getByClassCode(@PathVariable String classCode){
        QueryWrapper<TcClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_code", classCode);
        return classService.getOne(queryWrapper);
    }

    /**
     * 批量删除班级
     */
    @DeleteMapping("batchDelete")
    public Map<String, Object> batchDelete(@RequestBody List<Integer> ids){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = classService.removeByIds(ids);
            result.put("success", success);
            result.put("message", success ? "批量删除成功" : "批量删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除失败：" + e.getMessage());
        }
        return result;
    }
}
